package com.app.budgetbook;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import com.app.budgetbook.db.*;
import com.app.budgetbook.model.*;


public class Display extends Activity{

	protected static final int DATE_DIALOG_ID = 0;
	private EditText food;
    private EditText transport;
    private EditText health;
    private EditText accessories;
    private EditText family;
    private EditText entertainment;
    private Button change;
    private Long mRowId;
    private dbAdapter mDbHelper;
    private String dateNow;
    private TextView mDateDisplay;
    private int mYear;
    private int mMonth;
    private int mDay;

    
    private Budgetbook budget = null;

    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        mDbHelper = new dbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.newexpense);
       
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= 
        new SimpleDateFormat("yyyy/MM/dd");
        String dateNow = formatter.format(currentDate.getTime());
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        food = (EditText) findViewById(R.id.etfood);
        transport = (EditText) findViewById(R.id.ettransport);
        health = (EditText) findViewById(R.id.ethealth);
        accessories = (EditText) findViewById(R.id.etaccessories);
        family = (EditText) findViewById(R.id.etfamily);
        entertainment = (EditText) findViewById(R.id.etentertainment);
        
        // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();

        // display the current date (this method is below)
     
        //mDateDisplay= (TextView)findViewById(R.id.dateDisplay);
        change = (Button) findViewById(R.id.dateDisplay);
        change.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        
        Button saveButton = (Button) findViewById(R.id.savebutton);
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        mRowId = (bundle == null) ? null : (Long) bundle.getSerializable(dbAdapter.KEY_ROWID);
        if (extras != null) {
            mRowId = extras.getLong(dbAdapter.KEY_ROWID);
        }
        populateFields();
        saveButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                setResult(RESULT_OK);
                finish();
            }

        });
        Button chartview = (Button)findViewById(R.id.dchartbutton);
        chartview.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				startActivity(new Intent("com.app.budgetbook.DCHART"));
				
			}
		});
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
            }
            return null;
        }
    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
    }
    
    private void populateFields() {

        if (mRowId != null) {
            Cursor todo = mDbHelper.fetchBudget(mRowId);
            startManagingCursor(todo);
                            // Month is 0 based so add 1
            mDateDisplay.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_Date)));                         
            food.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_Food)));
            transport.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_Transport)));
            health.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_health)));
            accessories.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_accessories)));
            family.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_family)));
            entertainment.setText(todo.getString(todo.getColumnIndexOrThrow(dbAdapter.KEY_entertainment)));
        }
    }

    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(dbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {

        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {

        super.onResume();
        populateFields();
    }

    private void saveState() {

        budget = new Budgetbook();
        budget.date = mDateDisplay.getText().toString();
        budget.food = Integer.parseInt(food.getText().toString());
        budget.transport = Integer.parseInt(transport.getText().toString());
        budget.health = Integer.parseInt(health.getText().toString());
        budget.accessories = Integer.parseInt(accessories.getText().toString());
        budget.entertainment = Integer.parseInt(entertainment.getText().toString());
        budget.family = Integer.parseInt(family.getText().toString());

        if (mRowId == null) {
            long id = mDbHelper.createBudget(budget);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateBudget(mRowId, budget);
        }
    }
	

}
